package com.mioto.ld.module.alert.controller;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.aop.anno.BizLogAnno;
import com.mioto.ld.module.alert.model.AlertConfigDTO;
import com.mioto.ld.module.alert.model.AlertConfigDetailVO;
import com.mioto.ld.module.alert.model.AlertConfigPagerVO;
import com.mioto.ld.module.log.model.BizModuleName;
import com.mioto.ld.module.log.model.BizOperationType;
import org.springframework.web.bind.annotation.*;
import com.mioto.ld.module.alert.model.AlertConfig;
import com.mioto.ld.module.alert.service.IAlertConfigService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.exception.BasicException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 *
 * @author mioto
 * @date 2024-05-20 23:49:39
 */
@RestController
@RequestMapping("alertConfig")
@Api(tags = "告警配置信息管理")
public class AlertConfigController {
    @Resource
    private IAlertConfigService alertConfigService;

    @GetMapping("/pager")
    @ApiOperation(value="分页查询告警配置信息",response = AlertConfigPagerVO.class)
    @BizLogAnno(moduleName = BizModuleName.ALERT,operationType = BizOperationType.PAGER)
    public ResultData pager (HttpServletRequest request, AlertConfig alertConfig,BasePager basePager){
        int count = alertConfigService.findCount(alertConfig);
        Map<String, Object> result = new HashMap<>(4);
        List<AlertConfigPagerVO> list = new ArrayList<>(0);
        if (count > 0) {
            list = alertConfigService.pager(alertConfig,basePager);
        }
        result.put("count", count);
        result.put("list", list);
        return ResultData.success(result);
    }

    @GetMapping("/detail")
    @ApiOperation(value="查询告警配置信息",response = AlertConfigDetailVO.class)
    @BizLogAnno(moduleName = BizModuleName.ALERT,operationType = BizOperationType.DETAIL)
    public ResultData findDetail (int id){
        return ResultData.success(alertConfigService.findDetail(id));
    }

    @PostMapping
    @ApiOperation(value="新增告警配置信息")
    @BizLogAnno(moduleName = BizModuleName.ALERT,operationType = BizOperationType.INSERT)
    public ResultData add (@RequestBody AlertConfigDTO alertConfig){
        return Optional.of(alertConfigService.insert(alertConfig))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(alertConfig))
                .orElseThrow(() -> new BasicException(SystemTip.INSERT_FAIL));
    }

    @PatchMapping
    @ApiOperation(value="更新告警配置信息 局部更新,忽略空值")
    @BizLogAnno(moduleName = BizModuleName.ALERT,operationType = BizOperationType.UPDATE)
    public ResultData updateIgnoreNull (@RequestBody AlertConfigDTO alertConfig){
        return Optional.of(alertConfigService.updateIgnoreNull(alertConfig))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(alertConfig))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @DeleteMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名删除告警配置信息")
    @BizLogAnno(moduleName = BizModuleName.ALERT,operationType = BizOperationType.DELETE)
    public ResultData deleteByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return Optional.of(alertConfigService.deleteByColumn(StrUtil.toUnderlineCase(column),value))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

}